package top.lyooo.mybatislite.util;

import top.lyooo.mybatislite.Annotation.DbField;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author :LiYu
 * @date :2020-08-25 20:44
 * @desc :
 * @return :
 */
public class TransformUtils {
    public static Map toMap(Object... key_value) {
        Object key = null;
        Object value = null;
        HashMap<Object, Object> map = new HashMap<>();
        for (int i = 0; i < key_value.length; i++) {
            if (i % 2 == 0) {
                key = key_value[i];
            } else {
                value = key_value[i];
                map.put(key, value);
            }
        }
        return map;
    }
    public static <T> List toList(T... ts) {
        ArrayList<T> list = new ArrayList<>();
        for (T t : ts) {
            list.add(t);
        }
        return list;
    }

    public static <T> T mapToBean(Map<String, Object> map, Class<T> cls) {
        T t = null;
        try {
            t = cls.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Field[] fs = cls.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            String key = null;
            if (f.isAnnotationPresent(DbField.class)) {
                key = f.getAnnotation(DbField.class).value();
            }else {
                key = f.getName();
            }
            Object value = map.get(key);
            f.setAccessible(true);
            try {
                if (value instanceof String) {
                    f.set(t, value.toString());
                }
                if (value instanceof Byte) {
                    f.set(t, Byte.valueOf(value.toString()));
                }
                if (value instanceof Integer) {
                    f.set(t, Integer.parseInt(value.toString()));
                }
                if (value instanceof Long) {
                    f.set(t, Long.valueOf(value.toString()));
                }
                if (value instanceof Float) {
                    f.set(t, Float.valueOf(value.toString()));
                }
                if (value instanceof Double) {
                    f.set(t, Double.valueOf(value.toString()));
                }
                if (value instanceof Date) {
                    try {
                        f.set(t, (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(value.toString()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                if (value instanceof java.sql.Date) {
                    try {
                        f.set(t, new java.sql.Date(((new SimpleDateFormat("yyyy-MM-dd")).parse(value.toString())).getTime()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                if (value instanceof java.sql.Date) {
                    try {
                        f.set(t, new java.sql.Time(((new SimpleDateFormat("HH:mm:ss")).parse(value.toString())).getTime()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                if (value instanceof LocalDate) {
                    f.set(t,LocalDate.parse(value.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }
                if (value instanceof LocalDateTime) {
                    f.set(t,LocalDate.parse(value.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                }
                if (value instanceof LocalTime) {
                    f.set(t,LocalDate.parse(value.toString(), DateTimeFormatter.ofPattern("HH:mm:ss")));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return t;
    }
}
